package com.neusoft.elmboot.util;

import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import javax.crypto.Cipher;
import java.util.Base64;

public class RSAUtil {

    public static String encryptMessageWithPublicKey(String publicKeyStr, String message) {
        try {
            // 将公钥字符串转换为 PublicKey 对象
            byte[] publicKeyBytes = Base64.getDecoder().decode(publicKeyStr);
            X509EncodedKeySpec keySpec = new X509EncodedKeySpec(publicKeyBytes);
            KeyFactory keyFactory = KeyFactory.getInstance("RSA");
            PublicKey publicKey = keyFactory.generatePublic(keySpec);

            // 使用 RSA 进行加密
            Cipher cipher = Cipher.getInstance("RSA");
            cipher.init(Cipher.ENCRYPT_MODE, publicKey);
            byte[] encryptedMessageBytes = cipher.doFinal(message.getBytes("UTF-8"));

            // 返回加密后的 Base64 编码字符串
            return Base64.getEncoder().encodeToString(encryptedMessageBytes);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static String decryptMessageWithPrivateKey(String privateKeyStr, String encryptedMessage) {
        try {
            // 将私钥字符串转换为 PrivateKey 对象
            byte[] privateKeyBytes = Base64.getDecoder().decode(privateKeyStr);
            PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(privateKeyBytes);
            KeyFactory keyFactory = KeyFactory.getInstance("RSA");
            PrivateKey privateKey = keyFactory.generatePrivate(keySpec);
            
            // 将加密的消息从 Base64 解码
            byte[] encryptedMessageBytes = Base64.getDecoder().decode(encryptedMessage);
            
            // 使用 RSA 进行解密
            Cipher cipher = Cipher.getInstance("RSA");
            cipher.init(Cipher.DECRYPT_MODE, privateKey);
            byte[] decryptedMessageBytes = cipher.doFinal(encryptedMessageBytes);
            
            // 将解密后的消息转换为字符串
            return new String(decryptedMessageBytes, "UTF-8");
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static KeyPair generateKeyPair() {
        try {
            // 创建 RSA 密钥对生成器
            KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
            
            // 初始化密钥对生成器，使用 2048 位密钥
            keyPairGenerator.initialize(2048);
            
            // 生成并返回密钥对
            return keyPairGenerator.generateKeyPair();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

//    public static void main(String[] args) {
//        try {
//            // 生成 RSA 密钥对
//            KeyPair keyPair = generateKeyPair();
//            
//            if (keyPair != null) {
//                // 打印公钥和私钥的算法名称以确认
//                System.out.println("公钥算法: " + keyPair.getPublic().getAlgorithm());
//                System.out.println("私钥算法: " + keyPair.getPrivate().getAlgorithm());
//                
//                // 打印编码后的公钥和私钥
//                System.out.println("公钥: " + Base64.getEncoder().encodeToString(keyPair.getPublic().getEncoded()));
//                System.out.println("私钥: " + Base64.getEncoder().encodeToString(keyPair.getPrivate().getEncoded()));
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }
}
